<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

class DataAction extends AccountAction
{
	public function index($value='')
	{
		redirect('/data/sync');
	}

	public function sync()
	{
		$users = array();
		$page = I('page');
		(int)$page <= 0 && $page = 1;

		$pageSize = 50;
		$size = ($page-1)*$pageSize;


		$order = strtolower(I('order', ''));
		// $orderStatus = false;
		switch ($order) {
			case 'campaign':
			case 'adgroup':
			case 'keyword':
			case 'creative':
			case 'recommends':
			case 'insightdata':
				break;
			default:
				$order = 'campaign';
				break;
		}

		$asc = strtolower(I('asc'));
		$asc = $asc == 'asc' ? 'asc' : 'desc';
		
		$search = I('search');
		$params = 'u.item_code != \'\'';

		if(isset($search['from']) && $search['from']) {
			$from = trim($search['from']);
			unset($search['from']);
		} else {
			$from = date('Y-m-d', strtotime('-7 day'));
		}

		if(isset($search['to']) && $search['to']) {
			$to = trim($search['to']);
			unset($search['to']);
		} else {
			$to = date('Y-m-d', strtotime('-1 day'));
		}
		$dateTime = " AND s.sync_date BETWEEN '{$from}' AND '{$to}'";

		if(isset($search['nick']) && $search['nick']) {
			$nick = trim($search['nick']);
			$this->assign('nick', $nick);
			$params .= " AND u.nick LIKE '%{$nick}%'";
			unset($nick);
		}

		$tmp = M()->table(array('users'=>'u'))->field('user_id,nick,null date')->where($params)->select();
		
		if(count($tmp) > 0) {
			foreach($tmp as $r){
				if( !isset($r['user_id']) ) break;
				$users[$r['user_id']] = $r;
			}

			$rules = C('ACCOUNT_SYNC_FIELD');
			foreach ($rules as $key => $param) {
				$res = M()->field('u.user_id,u.nick,COUNT(s.user_id) num,s.sync_date')->table('users u')->where($params . ' AND ' . $param . $dateTime)->join('LEFT JOIN synclog s ON s.user_id = u.user_id')->group('u.user_id')->select();
				// dump(M()->getLastSql());
				// continue;
				if($res) {
					foreach ($res as $k => $v) {
						if(strtotime($users[$v['user_id']]['date']) < strtotime($v['sync_date']))
							$users[$v['user_id']]['date'] = $v['sync_date'];
						$users[$v['user_id']][$key] = $v['num'];
					}
				}
			}

			foreach ($users as &$user) {
				foreach ($rules as $key => $param) {
					!isset($user[$key]) ? ($user[$key] = 0) : ($user[$key] = (int)$user[$key]);
				}
			}
		}
		$totalCount = count($users);
		$totalPage = ceil($totalCount/$pageSize);

		$func = "uc_cmp_{$order}_{$asc}";
		if($order)  {
			usort($users, $func);
		}

		$users = array_slice($users, $size, $pageSize);


		$query = array();
		if(I('search'))
			$query['search'] = I('search');
		$query['order'] = $order;
		$query['asc'] = $asc;

		$this->assign('users', $users);
		$this->assign('totalPage', $totalPage);
		$this->assign('lastPage', $page <= 1 ? 1 : $page-1);
		$this->assign('currentPage', $page);
		$this->assign('nextPage', $page >= $totalPage ? $totalPage : $page+1);
		$this->assign('current_search', $query ? ('&' . http_build_query($query)) : '');
		$this->assign('order', $order);
		$this->assign('asc', strtoupper($asc));
		$this->assign('dateTime', array('from'=> $from, 'to'=> $to));

		$this->display();
	}
}